// index.ts
// 获取应用实例
import { request, api } from '../../utils/request'

const app = getApp<IAppOption>()

Page({
  data: {
    isLoading: false,
    goodsList: [],
    bannerList: [],
    page: 1,
    pageSize: 10,
    totalPage: 0
  },
  async getGoodsList(callback) {
    this.setData({
      isLoading: true
    })
    let res = await request(api.GoodsList, {
      isNew: true,
      page: this.data.page,
      size: this.data.pageSize,
      // order: this.data.currentSortOrder,
      // sort: this.data.currentSort,
      // categoryId: this.data.categoryId
    })

    if (res.errno === 0) {
      this.setData({
        totalPage: res.data.totalPage,
        goodsList: [...this.data.goodsList, ...res.data.data],
      });
    }
    this.setData({
      isLoading: false
    })
    callback && callback()
  },
  async getBannerList() {
    let res = await request(api.BannerList)
    // console.log(res);

    if (res.errno === 0) {
      this.setData({
        bannerList: res.data
      })
    }
  },
  onLoad() {
    this.getBannerList()
    this.getGoodsList()
  },
  onReachBottom() {
    if (this.data.page >= this.data.totalPage) {
      return wx.showToast({
        title: '到底了！',
        icon: 'none'
      })
    }
    if (this.data.isLoading) {
      return
    }
    this.setData({
      page: this.data.page + 1
    })
    this.getGoodsList()
  },

  onPullDownRefresh() {
    this.setData({
      page: 1,
      goodsList: [],
      totalPage: 0
    })
    this.getGoodsList(wx.stopPullDownRefresh())
  }
})
